//
//  MainMenuLayer.m
//  Template
//
//  Created by Hilary Muludiang on 7/1/12.
//  Copyright 2012 Versatile Systems, Inc. All rights reserved.
//

#import "MainMenuLayer.h"

//Private methods
@interface MainMenuLayer()
-(void)displayMainMenu;
-(void)displaySceneSelection;
@end


@implementation MainMenuLayer

-(void)showCredits{
	CCLOG(@"Show the Credits screen");
	[[GameManager sharedGameManager] runSceneWithID:kCreditsScene];
}

-(void)showOptions{
	CCLOG(@"Show the Options screen");
	[[GameManager sharedGameManager] runSceneWithID:kOptionsScene];
}

-(void)playScene:(CCMenuItemFont *)itemPassedIn{
	if ([itemPassedIn tag] == 1){
		CCLOG(@"Tag 1 found, Scene 1");
		[[GameManager sharedGameManager] runSceneWithID:kIntroScene];
	} else {
		CCLOG(@"Tag was: %d", [itemPassedIn tag]);
		CCLOG(@"Placeholder for next levels");
	}
}

-(void)displayMainMenu {
	CGSize screenSize = [CCDirector sharedDirector].winSize;
	if (sceneSelectMenu != nil) {
		[sceneSelectMenu removeFromParentAndCleanup:YES];
	}
	// Main Menu
	/* Using an image => later
	 
	 CCMenuItemImage *playGameButton = [CCMenuItemImage
									   itemFromNormalImage:@"PlayGameButtonNormal.png"
									   selectedImage:@"PlayGameButtonSelected.png"
									   disabledImage:nil
									   target:self
									   selector:@selector(displaySceneSelection)];*/
	
	//Using fonts
	CCMenuItemFont *playButton = [CCMenuItemFont itemFromString: @"Play Game" target: self selector:@selector(displaySceneSelection)];
	CCMenuItemFont *optionsButton = [CCMenuItemFont itemFromString: @"Game Options" target: self selector:@selector(showOptions)];
	CCMenuItemFont *creditsButton = [CCMenuItemFont itemFromString: @"Credits" target: self selector:@selector(showCredits)];
	mainMenu = [CCMenu menuWithItems:playButton, optionsButton, creditsButton, nil];
	[mainMenu alignItemsVerticallyWithPadding:screenSize.height * 0.59f];
	[mainMenu setPosition:ccp(screenSize.width * 2, screenSize.height /2)];
	id moveAction = [CCMoveTo actionWithDuration:1.2f position:ccp(screenSize.width * 0.85f, screenSize.height/2)];
	id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
	[mainMenu runAction:moveEffect];
	[self addChild:mainMenu z:0 tag:kMainMenuTagValue];
}

-(void)displaySceneSelection {
	CGSize screenSize = [CCDirector sharedDirector].winSize;
	if (mainMenu != nil) {
		[mainMenu removeFromParentAndCleanup:YES];
	}
	CCMenuItemFont *playLevel1 = [CCMenuItemFont itemFromString: @"Level 1" target:self selector:@selector(playScene:)];
	[playLevel1 setTag:1];
	
		
	sceneSelectMenu = [CCMenu menuWithItems:playLevel1,nil];
	[sceneSelectMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
	[sceneSelectMenu setPosition:ccp(screenSize.width * 2, screenSize.height / 2)];
	id moveAction = [CCMoveTo actionWithDuration:0.5f position:ccp(screenSize.width * 0.75f, screenSize.height/2)];
	id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
	[sceneSelectMenu runAction:moveEffect];
	[self addChild:sceneSelectMenu z:1 tag:kSceneMenuTagValue];
}

-(id)init {
	self = [super init];
	if (self != nil) {
		CGSize screenSize = [CCDirector sharedDirector].winSize;
		/*CCSprite *background =
		[CCSprite spriteWithFile:@"MainMenuBackground.png"];
		[background setPosition:ccp(screenSize.width/2,
									screenSize.height/2)];
		[self addChild:background];
		[self displayMainMenu];
		CCSprite *viking =
		[CCSprite spriteWithFile:@"VikingFloating.png"];
		[viking setPosition:ccp(screenSize.width * 0.35f,
								screenSize.height * 0.45f)];
		[self addChild:viking];
		id rotateAction = [CCEaseElasticInOut actionWithAction:
						   [CCRotateBy actionWithDuration:5.5f
													angle:360]];
		id scaleUp = [CCScaleTo actionWithDuration:2.0f scale:1.5f];
		id scaleDown = [CCScaleTo actionWithDuration:2.0f scale:0.5f];
		[viking runAction:[CCRepeatForever actionWithAction:
						   [CCSequence
							actions:scaleUp,scaleDown,nil]]];
		[viking runAction:
		 [CCRepeatForever actionWithAction:rotateAction]];*/
		[self displayMainMenu];
	}
	return self;
}

@end
